define(['models/geolocation', 'models/map', 'models/position', 'drawing/route', '/socket.io/socket.io.js'], function (geolocation, map, Position, Route) {
    "use strict";

    function Opener() {
        this.id;
        this.socket;
        this.connect();
        this.position;
        this.route;
    };

    Opener.prototype = {
        constructor: Opener,
        dispose: function () {
            geolocation.dispose();
        },
        getCurrentPosition: function () {
            geolocation.getCurrentPosition(this.success.bind(this), this.error.bind(this));
        },
        start: function () {
            console.log('models/user/opener.js. start.');

            this.route = new Route();
            if (this.position) this.route.addPosition(this.position);
            var self = this;
            this.intervalId = setInterval(function () {
                console.log('models/user/opener.js. setInterval.')
                self.position.longitude += 0.001;
                self.position.latitude += 0.001;
                self.route.addPosition(self.position);
            }, 2000);

            //geolocation.watchPosition(this.success.bind(this), this.error.bind(this));
        },
        stop: function () {
            console.log('models/user/opener.js. stop.');
            clearInterval(this.intervalId);
            //geolocation.stopWatchPosition();
        },
        success: function (data) {
            this.position = new Position(data, this.id);
            var marker = map.updateMarker(this.position);
            if (marker.id === this.id) map.panTo(marker);
            if (this.route) this.route.addPosition(this.position);

        },
        error: function () {
        },
        connect: function () {
            this.socket = io.connect();
            self = this;
            this.socket.on('connect', function () {
                self.id = this.socket.sessionid;
                console.log('models/user/opener.js. socket.io connect id: ' + self.id);
                self.getCurrentPosition();
            });
        }
    };

    console.log('models/user/opener.js');
    return new Opener();
});